let util = require('../../../../../common/util/util')

Page({
    data: {
        use: [],
        xDefaultValue : '' ,
        value : ''
    },

    onShow: function () {
        this.xInit()
    },

    xInit: function () {
        let that = this
        wx.cloud.callFunction({
            name: 'digital_therapy',
            data: {
                action: "get_toBeCommented_task_list"
            }
        }).then(res => {
            console.log("返回数据:", res)
            let data = res.result
            for (var i in data) {
                data[i].begin_time = util.formatDate_chinese(new Date(data[i].begin_time))
            }
            that.setData({
                use: data
            })
        })
    },

    xToDet : function (e) {
        let params = e.currentTarget.dataset
        wx.navigateTo({
            url: '/subdter/pages/mine/xtask/detail/index',
            success: (res) => {
                res.eventChannel.emit('xToDetail', params)
            }
        })
    },

    voice: function () {
        var that = this
        let id = that.data.id
        that.setData({
            record: 1 ,
            xMcShow : true
        })
        recordTimeInterval = setInterval(function () { //计算时分秒
            var recordTime = that.data.recordTime += 1
            that.setData({
                formatedRecordTime: util.formatvoiceTime(that.data.recordTime),
                recordTime: recordTime
            })
        }, 1000)
        const options = {
            duration: 30000,
            sampleRate: 44100,
            numberOfChannels: 1,
            encodeBitRate: 192000,
            format: 'mp3'
        }
        radio.start(options);
        radio.onStart(res => {
            console.log('监听录音', res)
        });
    },

    voiced: function () {
        console.log('录音结束了')
        let that = this;
        let id = that.data.id
        clearInterval(recordTimeInterval);
        that.setData({
            xMcShow : false
        })
        radio.stop(); //录音结束
        radio.onStop((res) => { //录音结束
            // that.stopRecord
            console.log('录音结束', res);
            this.setData({
                record: res.tempFilePath,
                musicUrl: res.tempFilePath, //录音音频
                duration: res.duration, //音频时长时间戳
                record: true,
            })
            if (res.duration < 1000) {
                wx.showToast({
                    title: '说的太快了',
                    icon: 'none'
                })
            } else {
                let url = ''
                wx.cloud.uploadFile({
                    cloudPath: 'profile/' + id + "/" + new Date().getTime() + '.mp3',
                    filePath: res.tempFilePath
                }).then(xRes => {
                    wx.cloud.getTempFileURL({
                        fileList: [xRes.fileID],
                        success: xDoRes => {
                            console.log(xDoRes)
                            let url = xDoRes.fileList[0].tempFileURL
                            let _dataObj = {
                                to_openid: id,
                                message_type: 3,
                                business_type: 6,
                                content: {
                                    title: 'voice',
                                    content: {
                                        pic: "",
                                        text: "",
                                        audio: {
                                            record: res.tempFilePath,
                                            musicUrl: url, //录音音频
                                            duration: res.duration, //音频时长时间戳
                                            record: true,
                                        },
                                        parameter: ""
                                    }
                                }
                            }
                            wx.cloud.callFunction({
                                name: 'consulting_message',
                                data: {
                                    action: "send_message",
                                    dataObj: _dataObj
                                }
                            }).then(res => {
                                wx.showToast({
                                    title: '发送成功',
                                    icon: 'none'
                                })
                                that.setData({
                                    xDefaultValue: ''
                                })
                                that.xQuiz()
                            })
                        }
                    })
                })
            }
        })
    },

    player: function (e) {
        let that = this
        let params = e.currentTarget.dataset
        console.log(that.data.player)
        that.setData({
            playStatus: false
        })
        var musicUrl = params.vocd
        console.log(params)
        console.log('musicUrl', musicUrl)
        innerAudioContext.src = musicUrl;
        innerAudioContext.autoplay = true
        innerAudioContext.play();
        console.log(that.data.player)
        innerAudioContext.onTimeUpdate((res) => {
            console.log('onTimeUpdate', res)
            console.log("duratio的值：", innerAudioContext.duration)
            that.setData({
                duration: innerAudioContext.duration.toFixed(2) * 100,
                curTimeVal: innerAudioContext.currentTime.toFixed(2) * 100,
                formatedPlayTime: this.formatvoiceTime(innerAudioContext.currentTime)
            })
            if (innerAudioContext.duration.toFixed(2) - innerAudioContext.currentTime.toFixed(2) <= 0) {
                that.setStopState(that)
                console.log(1)
            }
            innerAudioContext.onEnded(() => {
                console.log(1)
                that.setStopState(that)
            })
        })
    },

    input: function (e) {
        let that = this
        let value = e.detail.value
        that.setData({
            value: value
        })
    },

    sub: function (e) {
        let that = this
        let value = that.data.value
        let id = e.currentTarget.dataset.id
        if (value === '') {
            wx.showToast({
                title: '请输入内容',
                icon: 'none'
            })
        } else {
            let _objData = {
                _id : id, 
                summary: [
                     {
                       type : 1,
                       content : value
                     }
                 ]
             }
             wx.cloud.callFunction({
                 name: 'digital_therapy',
                 data: {
                     action: "add_interview_summary",
                     objData: _objData
                 }
             }).then(res => {
                wx.showToast({
                    title: '发送成功',
                    icon: 'none'
                })
                that.setData({
                    xDefaultValue: ''
                })
                that.xInit()
            })
        }
    },
})